
#include "stdafx.h"

#include "PropertiesWnd.h"
#include "Resource.h"
#include "MainFrm.h"
#include "MFCD3D11Test.h"
#include "MFCD3D11TestView.h"

#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif

/////////////////////////////////////////////////////////////////////////////
// CResourceViewBar

CPropertiesWnd::CPropertiesWnd()
{
}

CPropertiesWnd::~CPropertiesWnd()
{
}

BEGIN_MESSAGE_MAP(CPropertiesWnd, CDockablePane)
	ON_WM_CREATE()
	ON_WM_SIZE()
	ON_COMMAND(ID_EXPAND_ALL, OnExpandAllProperties)
	ON_UPDATE_COMMAND_UI(ID_EXPAND_ALL, OnUpdateExpandAllProperties)
	ON_COMMAND(ID_SORTPROPERTIES, OnSortProperties)
	ON_UPDATE_COMMAND_UI(ID_SORTPROPERTIES, OnUpdateSortProperties)
	ON_COMMAND(ID_PROPERTIES1, OnProperties1)
	ON_UPDATE_COMMAND_UI(ID_PROPERTIES1, OnUpdateProperties1)
	ON_COMMAND(ID_PROPERTIES2, OnProperties2)
	ON_UPDATE_COMMAND_UI(ID_PROPERTIES2, OnUpdateProperties2)
	ON_WM_SETFOCUS()
	ON_WM_SETTINGCHANGE()
	ON_REGISTERED_MESSAGE(AFX_WM_PROPERTY_CHANGED, OnPropertyChanged)
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CResourceViewBar message handlers

void CPropertiesWnd::AdjustLayout()
{
	if (GetSafeHwnd() == NULL)
	{
		return;
	}

	CRect rectClient,rectCombo;
	GetClientRect(rectClient);

	m_wndObjectCombo.GetWindowRect(&rectCombo);

	int cyCmb = rectCombo.Size().cy;
	int cyTlb = m_wndToolBar.CalcFixedLayout(FALSE, TRUE).cy;

	m_wndObjectCombo.SetWindowPos(NULL, rectClient.left, rectClient.top, rectClient.Width(), 200, SWP_NOACTIVATE | SWP_NOZORDER);
	m_wndToolBar.SetWindowPos(NULL, rectClient.left, rectClient.top + cyCmb, rectClient.Width(), cyTlb, SWP_NOACTIVATE | SWP_NOZORDER);
	m_wndPropList.SetWindowPos(NULL, rectClient.left, rectClient.top + cyCmb + cyTlb, rectClient.Width(), rectClient.Height() -(cyCmb+cyTlb), SWP_NOACTIVATE | SWP_NOZORDER);
}

int CPropertiesWnd::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
	if (CDockablePane::OnCreate(lpCreateStruct) == -1)
		return -1;

	CRect rectDummy;
	rectDummy.SetRectEmpty();

	// Create combo:
	const DWORD dwViewStyle = WS_CHILD | WS_VISIBLE | CBS_DROPDOWNLIST | WS_BORDER | CBS_SORT | WS_CLIPSIBLINGS | WS_CLIPCHILDREN;

	if (!m_wndObjectCombo.Create(dwViewStyle, rectDummy, this, 1))
	{
		TRACE0("Failed to create Properties Combo \n");
		return -1;      // fail to create
	}

	m_wndObjectCombo.AddString(_T("Application"));
	m_wndObjectCombo.AddString(_T("Properties Window"));
	m_wndObjectCombo.SetCurSel(0);

	if (!m_wndPropList.Create(WS_VISIBLE | WS_CHILD, rectDummy, this, 2))
	{
		TRACE0("Failed to create Properties Grid \n");
		return -1;      // fail to create
	}

	InitPropList();

	m_wndToolBar.Create(this, AFX_DEFAULT_TOOLBAR_STYLE, IDR_PROPERTIES);
	m_wndToolBar.LoadToolBar(IDR_PROPERTIES, 0, 0, TRUE /* Is locked */);
	m_wndToolBar.CleanUpLockedImages();
	m_wndToolBar.LoadBitmap(theApp.m_bHiColorIcons ? IDB_PROPERTIES_HC : IDR_PROPERTIES, 0, 0, TRUE /* Locked */);

	m_wndToolBar.SetPaneStyle(m_wndToolBar.GetPaneStyle() | CBRS_TOOLTIPS | CBRS_FLYBY);
	m_wndToolBar.SetPaneStyle(m_wndToolBar.GetPaneStyle() & ~(CBRS_GRIPPER | CBRS_SIZE_DYNAMIC | CBRS_BORDER_TOP | CBRS_BORDER_BOTTOM | CBRS_BORDER_LEFT | CBRS_BORDER_RIGHT));
	m_wndToolBar.SetOwner(this);

	// All commands will be routed via this control , not via the parent frame:
	m_wndToolBar.SetRouteCommandsViaFrame(FALSE);

	AdjustLayout();
	return 0;
}

void CPropertiesWnd::OnSize(UINT nType, int cx, int cy)
{
	CDockablePane::OnSize(nType, cx, cy);
	AdjustLayout();
}

void CPropertiesWnd::OnExpandAllProperties()
{
	m_wndPropList.ExpandAll();
}

void CPropertiesWnd::OnUpdateExpandAllProperties(CCmdUI* /* pCmdUI */)
{
}

void CPropertiesWnd::OnSortProperties()
{
	m_wndPropList.SetAlphabeticMode(!m_wndPropList.IsAlphabeticMode());
}

void CPropertiesWnd::OnUpdateSortProperties(CCmdUI* pCmdUI)
{
	pCmdUI->SetCheck(m_wndPropList.IsAlphabeticMode());
}

void CPropertiesWnd::OnProperties1()
{
	// TODO: Add your command handler code here
}

void CPropertiesWnd::OnUpdateProperties1(CCmdUI* /*pCmdUI*/)
{
	// TODO: Add your command update UI handler code here
}

void CPropertiesWnd::OnProperties2()
{
	// TODO: Add your command handler code here
}

void CPropertiesWnd::OnUpdateProperties2(CCmdUI* /*pCmdUI*/)
{
	// TODO: Add your command update UI handler code here
}

void CPropertiesWnd::InitPropList()
{
	SetPropListFont();

	m_wndPropList.EnableHeaderCtrl(FALSE);
	m_wndPropList.EnableDescriptionArea();
	m_wndPropList.SetVSDotNetLook();
	m_wndPropList.MarkModifiedProperties();

	/*
	CMFCPropertyGridProperty* pGroup1 = new CMFCPropertyGridProperty(_T("Appearance"));

	pGroup1->AddSubItem(new CMFCPropertyGridProperty(_T("3D Look"), (_variant_t) false, _T("Specifies the window's font will be non-bold and controls will have a 3D border")));

	CMFCPropertyGridProperty* pProp = new CMFCPropertyGridProperty(_T("Border"), _T("Dialog Frame"), _T("One of: None, Thin, Resizable, or Dialog Frame"));
	pProp->AddOption(_T("None"));
	pProp->AddOption(_T("Thin"));
	pProp->AddOption(_T("Resizable"));
	pProp->AddOption(_T("Dialog Frame"));
	pProp->AllowEdit(FALSE);

	pGroup1->AddSubItem(pProp);
	pGroup1->AddSubItem(new CMFCPropertyGridProperty(_T("Caption"), (_variant_t) _T("About"), _T("Specifies the text that will be displayed in the window's title bar")));

	m_wndPropList.AddProperty(pGroup1);

	CMFCPropertyGridProperty* pSize = new CMFCPropertyGridProperty(_T("Window Size"), 0, TRUE);

	pProp = new CMFCPropertyGridProperty(_T("Height"), (_variant_t) 250l, _T("Specifies the window's height"));
	pProp->EnableSpinControl(TRUE, 50, 300);
	pSize->AddSubItem(pProp);

	pProp = new CMFCPropertyGridProperty( _T("Width"), (_variant_t) 150l, _T("Specifies the window's width"));
	pProp->EnableSpinControl(TRUE, 50, 200);
	pSize->AddSubItem(pProp);

	m_wndPropList.AddProperty(pSize);

	CMFCPropertyGridProperty* pGroup2 = new CMFCPropertyGridProperty(_T("Font"));

	LOGFONT lf;
	CFont* font = CFont::FromHandle((HFONT) GetStockObject(DEFAULT_GUI_FONT));
	font->GetLogFont(&lf);

	lstrcpy(lf.lfFaceName, _T("Arial"));

	pGroup2->AddSubItem(new CMFCPropertyGridFontProperty(_T("Font"), lf, CF_EFFECTS | CF_SCREENFONTS, _T("Specifies the default font for the window")));
	pGroup2->AddSubItem(new CMFCPropertyGridProperty(_T("Use System Font"), (_variant_t) true, _T("Specifies that the window uses MS Shell Dlg font")));

	m_wndPropList.AddProperty(pGroup2);

	CMFCPropertyGridProperty* pGroup3 = new CMFCPropertyGridProperty(_T("Misc"));
	pProp = new CMFCPropertyGridProperty(_T("(Name)"), _T("Application"));
	pProp->Enable(FALSE);
	pGroup3->AddSubItem(pProp);

	CMFCPropertyGridColorProperty* pColorProp = new CMFCPropertyGridColorProperty(_T("Window Color"), RGB(210, 192, 254), NULL, _T("Specifies the default window color"));
	pColorProp->EnableOtherButton(_T("Other..."));
	pColorProp->EnableAutomaticButton(_T("Default"), ::GetSysColor(COLOR_3DFACE));
	pGroup3->AddSubItem(pColorProp);

	static const TCHAR szFilter[] = _T("Icon Files(*.ico)|*.ico|All Files(*.*)|*.*||");
	pGroup3->AddSubItem(new CMFCPropertyGridFileProperty(_T("Icon"), TRUE, _T(""), _T("ico"), 0, szFilter, _T("Specifies the window icon")));

	pGroup3->AddSubItem(new CMFCPropertyGridFileProperty(_T("Folder"), _T("c:\\")));

	m_wndPropList.AddProperty(pGroup3);

	CMFCPropertyGridProperty* pGroup4 = new CMFCPropertyGridProperty(_T("Hierarchy"));

	CMFCPropertyGridProperty* pGroup41 = new CMFCPropertyGridProperty(_T("First sub-level"));
	pGroup4->AddSubItem(pGroup41);

	CMFCPropertyGridProperty* pGroup411 = new CMFCPropertyGridProperty(_T("Second sub-level"));
	pGroup41->AddSubItem(pGroup411);

	pGroup411->AddSubItem(new CMFCPropertyGridProperty(_T("Item 1"), (_variant_t) _T("Value 1"), _T("This is a description")));
	pGroup411->AddSubItem(new CMFCPropertyGridProperty(_T("Item 2"), (_variant_t) _T("Value 2"), _T("This is a description")));
	pGroup411->AddSubItem(new CMFCPropertyGridProperty(_T("Item 3"), (_variant_t) _T("Value 3"), _T("This is a description")));

	pGroup4->Expand(FALSE);
	m_wndPropList.AddProperty(pGroup4);*/

	CMFCPropertyGridProperty* pGroup1 = new CMFCPropertyGridProperty(_T("XYZFile Dim"));
	CMFCPropertyGridProperty* pGroupXYZFileDim_Width = new CMFCPropertyGridProperty(_T("Width:"), (_variant_t) 0.0f, _T("Display current XYZFile's width"));
	pGroupXYZFileDim_Width->AllowEdit(FALSE);
	pGroup1->AddSubItem(pGroupXYZFileDim_Width);
	
	CMFCPropertyGridProperty* pGroupXYZFileDim_Height = new CMFCPropertyGridProperty(_T("Height:"),(_variant_t) 0.0f, _T("Display current XYZFile's height"));
	pGroupXYZFileDim_Height->AllowEdit(FALSE);
	pGroup1->AddSubItem(pGroupXYZFileDim_Height);
	
	CMFCPropertyGridProperty* pGroupXYZFile_HMWidth = new CMFCPropertyGridProperty(_T("HMWidth:"), (_variant_t)(unsigned int)640, _T("HeightMap width."));
	pGroupXYZFile_HMWidth->AllowEdit(FALSE);
	pGroup1->AddSubItem(pGroupXYZFile_HMWidth);

	CMFCPropertyGridProperty* pGroupXYZFile_HMHeight = new CMFCPropertyGridProperty(_T("HMHeight:"), (_variant_t)(unsigned int)480, _T("HeightMap height."));
	pGroupXYZFile_HMHeight->AllowEdit(FALSE);
	pGroup1->AddSubItem(pGroupXYZFile_HMHeight);

	m_wndPropList.AddProperty(pGroup1);

	CMFCPropertyGridProperty* pGroup2 = new CMFCPropertyGridProperty(_T("Rendering Properties"));
	CMFCPropertyGridColorProperty* pNormalColorProp = new CMFCPropertyGridColorProperty(_T("Normal Color"), RGB(255, 255, 255), NULL, _T("Specifies the normal line color"));
	pNormalColorProp->EnableOtherButton(_T("Other..."));
	pNormalColorProp->EnableAutomaticButton(_T("Default"), ::GetSysColor(COLOR_3DFACE));
	pGroup2->AddSubItem(pNormalColorProp);

	CMFCPropertyGridProperty* pBoolRenderNormal = new CMFCPropertyGridProperty(_T("Show Normal"),(_variant_t) true, _T("Specifies whether show normal line"));
	pGroup2->AddSubItem(pBoolRenderNormal);

	CMFCPropertyGridProperty* pNormalLineScale = new CMFCPropertyGridProperty( _T("Line Scale"), (_variant_t) 10l, _T("Specifies the normal line scale"));
	pNormalLineScale->EnableSpinControl(TRUE, 1, 100);
	pGroup2->AddSubItem(pNormalLineScale);

	CMFCPropertyGridProperty* pBoolRenderWF = new CMFCPropertyGridProperty(_T("Wireframe"), (_variant_t) false, _T("Specifies whether render wireframe"));
	pGroup2->AddSubItem(pBoolRenderWF);

	m_wndPropList.AddProperty(pGroup2);

	CMFCPropertyGridProperty* pGroup3 = new CMFCPropertyGridProperty(_T("Normal Estimation"));

	CMFCPropertyGridProperty* pSelNormalEstiMethod = new CMFCPropertyGridProperty(_T("Estimation Method"), _T("NormalEstiMethod"), _T("One of: Least Square PCA Gaussian, Least Square PCA, or Triangle Area Average"));
	pSelNormalEstiMethod->AddOption(_T("LS PCA Gauss"));
	pSelNormalEstiMethod->AddOption(_T("LS PCA"));
	pSelNormalEstiMethod->AddOption(_T("TriArea Avg"));
	pSelNormalEstiMethod->AllowEdit(FALSE);
	pGroup3->AddSubItem(pSelNormalEstiMethod);
	
	CMFCPropertyGridProperty* pNormalFilterSize = new CMFCPropertyGridProperty( _T("PCA Kernel Size"), (_variant_t) 3l, _T("Specifies the local normal window's width"));
	pNormalFilterSize->EnableSpinControl(TRUE, 3, 99);
	pGroup3->AddSubItem(pNormalFilterSize);

	CMFCPropertyGridProperty* pNormalGSigmaSq = new CMFCPropertyGridProperty( _T("Gaussian Sigma"), (_variant_t) 6l, _T("Specifies the local normal filtering Gaussian Sigma"));
	pNormalGSigmaSq->EnableSpinControl(TRUE, 1, 100);
	pGroup3->AddSubItem(pNormalGSigmaSq);

	CMFCPropertyGridProperty* pSel1DGonioDir = new CMFCPropertyGridProperty(_T("1D Gonio Dir"), _T("1DGonioDir"), _T("Either MAJORAXIS or MINORAXIS"));
	pSel1DGonioDir->AddOption(_T("MAJORAXIS"));
	pSel1DGonioDir->AddOption(_T("MINORAXIS"));
	pSel1DGonioDir->AllowEdit(FALSE);
	pGroup3->AddSubItem(pSel1DGonioDir);


	m_wndPropList.AddProperty(pGroup3);

	
	CMFCPropertyGridProperty* pGroup4 = new CMFCPropertyGridProperty(_T("Display 2D NDF"));

	CMFCPropertyGridProperty* pBoolAvg1DNDF = new CMFCPropertyGridProperty(_T("Avg1DNDF"), (_variant_t) false, _T("Specifies whether average 1D NDF"));
	pGroup4->AddSubItem(pBoolAvg1DNDF);

	CMFCPropertyGridProperty* p1DNDFSliceLineOffsetY = new CMFCPropertyGridProperty( _T("1D_Offset_Y"), (_variant_t) 0l, _T("Specifies the offset for 1D ndf slice in Y dir"));
	p1DNDFSliceLineOffsetY->EnableSpinControl(TRUE, -100, 100);
	pGroup4->AddSubItem(p1DNDFSliceLineOffsetY);

	CMFCPropertyGridProperty* p1DNDFSliceLineRotateY = new CMFCPropertyGridProperty( _T("1D_Rotate_Y"), (_variant_t) 0l, _T("Specifies the rotation angle for 1D ndf slice in Y dir"));
	p1DNDFSliceLineRotateY->EnableSpinControl(TRUE, -450, 450);
	pGroup4->AddSubItem(p1DNDFSliceLineRotateY);

	CMFCPropertyGridProperty* p1DNDFSliceLineOffsetX = new CMFCPropertyGridProperty( _T("1D_Offset_X"), (_variant_t) 0l, _T("Specifies the offset for 1D ndf slice in X dir"));
	p1DNDFSliceLineOffsetX->EnableSpinControl(TRUE, -100, 100);
	pGroup4->AddSubItem(p1DNDFSliceLineOffsetX);

	CMFCPropertyGridProperty* p1DNDFSliceLineRotateX = new CMFCPropertyGridProperty( _T("1D_Rotate_X"), (_variant_t) 0l, _T("Specifies the rotation angle for 1D ndf slice in X dir"));
	p1DNDFSliceLineRotateX->EnableSpinControl(TRUE, -450, 450);
	pGroup4->AddSubItem(p1DNDFSliceLineRotateX);

	m_wndPropList.AddProperty(pGroup4);	

	CMFCPropertyGridProperty* pGroup5 = new CMFCPropertyGridProperty(_T("Ray tracing Heightmap"));
	
	CMFCPropertyGridProperty* pfRTScaleRatio = new CMFCPropertyGridProperty( _T("RTScaleRatio"), (_variant_t) 0l, _T("Specifies the scale ratio for the init ray generation plane"));
	pfRTScaleRatio->EnableSpinControl(TRUE, 0, 100);
	pGroup5->AddSubItem(pfRTScaleRatio);

	CMFCPropertyGridProperty* pfRTThetaAng = new CMFCPropertyGridProperty( _T("RTThetaAng"), (_variant_t) 0l, _T("Specifies theta angle for the ray"));
	pfRTThetaAng->EnableSpinControl(TRUE, -90, 90);
	pGroup5->AddSubItem(pfRTThetaAng);

	CMFCPropertyGridProperty* pfRTPhiAng = new CMFCPropertyGridProperty( _T("RTPhiAng"), (_variant_t) 0l, _T("Specifies phi angle for the ray"));
	pfRTPhiAng->EnableSpinControl(TRUE, 0, 180);
	pGroup5->AddSubItem(pfRTPhiAng);

	CMFCPropertyGridProperty* pFresnelReal = new CMFCPropertyGridProperty(_T("FresnelReal"), (_variant_t)(double)0.000, _T("Refractive index real part"));
	pGroup5->AddSubItem(pFresnelReal);

	CMFCPropertyGridProperty* pFresnelImg = new CMFCPropertyGridProperty(_T("FresnelImg"), (_variant_t)(double)0.000, _T("Refractive index imagine part"));
	pGroup5->AddSubItem(pFresnelImg);

	m_wndPropList.AddProperty(pGroup5);

	CMFCPropertyGridProperty* pGroup6 = new CMFCPropertyGridProperty(_T("Ellipsoid NDF Params"));
	CMFCPropertyGridProperty* pAlphaX_GGX = new CMFCPropertyGridProperty(_T("a_x_GGX"), (_variant_t)(double)0.000, _T("alpha_x for AGGX"));
	pGroup6->AddSubItem(pAlphaX_GGX);

	CMFCPropertyGridProperty* pAlphaY_GGX = new CMFCPropertyGridProperty(_T("a_y_GGX"), (_variant_t)(double)0.000, _T("alpha_y for AGGX"));
	pGroup6->AddSubItem(pAlphaY_GGX);

	CMFCPropertyGridProperty* pScaling_GGX = new CMFCPropertyGridProperty(_T("Scaling_GGX"),(_variant_t)(double)1.000, _T("scaling factor b for AGGX"));
	pGroup6->AddSubItem(pScaling_GGX);

	CMFCPropertyGridProperty* pAdjustScaling = new CMFCPropertyGridProperty(_T("AjustScalingFactor"),(_variant_t)(double)1.000,_T("scaling factor for adjustment"));
	pGroup6->AddSubItem(pAdjustScaling);

	CMFCPropertyGridProperty* pHMWidth = new CMFCPropertyGridProperty(_T("HMWidth"),(_variant_t)(unsigned int)640, _T("Width value of heightmap"));
	pGroup6->AddSubItem(pHMWidth);

	CMFCPropertyGridProperty* pHMHeight = new CMFCPropertyGridProperty(_T("HMHeight"),(_variant_t)(unsigned int)480, _T("Height value of heightmap"));
	pGroup6->AddSubItem(pHMHeight);

	CMFCPropertyGridProperty* pDataPSizeX = new CMFCPropertyGridProperty(_T("DataPSizeX"),(_variant_t)(double)70, _T("Physical X size of sample"));
	pGroup6->AddSubItem(pDataPSizeX);

	CMFCPropertyGridProperty* pDataPSizeY = new CMFCPropertyGridProperty(_T("DataPSizeY"),(_variant_t)(double)55, _T("Physical Y size of sample"));
	pGroup6->AddSubItem(pDataPSizeY);

	m_wndPropList.AddProperty(pGroup6);
}

void CPropertiesWnd::OnSetFocus(CWnd* pOldWnd)
{
	CDockablePane::OnSetFocus(pOldWnd);
	m_wndPropList.SetFocus();
}

void CPropertiesWnd::OnSettingChange(UINT uFlags, LPCTSTR lpszSection)
{
	CDockablePane::OnSettingChange(uFlags, lpszSection);
	SetPropListFont();
}

void CPropertiesWnd::SetPropListFont()
{
	::DeleteObject(m_fntPropList.Detach());

	LOGFONT lf;
	afxGlobalData.fontRegular.GetLogFont(&lf);

	NONCLIENTMETRICS info;
	info.cbSize = sizeof(info);

	afxGlobalData.GetNonClientMetrics(info);

	lf.lfHeight = info.lfMenuFont.lfHeight;
	lf.lfWeight = info.lfMenuFont.lfWeight;
	lf.lfItalic = info.lfMenuFont.lfItalic;

	m_fntPropList.CreateFontIndirect(&lf);

	m_wndPropList.SetFont(&m_fntPropList);
	m_wndObjectCombo.SetFont(&m_fntPropList);
}


LRESULT CPropertiesWnd::OnPropertyChanged( WPARAM wParam, LPARAM lParam )
{
	CMFCPropertyGridProperty* pProp = reinterpret_cast<CMFCPropertyGridProperty*>(lParam);
	//pProp->xxx
	CString strPropName = pProp->GetName();
	if(strPropName.Compare(_T("Normal Color")) == 0)
	{
		CMFCPropertyGridColorProperty* pColorProp = static_cast<CMFCPropertyGridColorProperty*>(pProp);
		COLORREF color = pColorProp->GetColor();
		byte r = GetRValue(color);
		byte g = GetGValue(color);
		byte b = GetBValue(color);
		D3DXVECTOR4 vColor = D3DXVECTOR4(r/255.0f,g/255.0f,b/255.0f,1.0f);	
		CMainFrame* pFrame = (CMainFrame*)AfxGetMainWnd();
		CView* pView = pFrame->GetActiveFrame()->GetActiveView();
		if(pView)
		{
			CMFCD3D11TestView* pCurView = (CMFCD3D11TestView*)(pView);
			pCurView->GetCurrentMainRender()->GetRenderHMTech()->SetNormalLineColor(vColor);
		}
	}
	//theApp.OutputString("")
	if (strPropName.Compare(_T("PCA Kernel Size")) == 0)
	{
		int iSize = pProp->GetValue().intVal;
		//odd always
		if(iSize%2==0)
			iSize++;
		CMainFrame* pFrame = (CMainFrame*)AfxGetMainWnd();
		CView* pView = pFrame->GetActiveFrame()->GetActiveView();
		if(pView)
		{
			CMFCD3D11TestView* pCurView = (CMFCD3D11TestView*)(pView);			
			pCurView->SetNormalWndSize(iSize);			
		}
		
	}
	if (strPropName.Compare(_T("Gaussian Sigma")) == 0)
	{
		int iSigma = pProp->GetValue().intVal;
		float fSigma = static_cast<float>(iSigma);
		
		CMainFrame* pFrame = (CMainFrame*)AfxGetMainWnd();
		CView* pView = pFrame->GetActiveFrame()->GetActiveView();
		if(pView)
		{
			CMFCD3D11TestView* pCurView = (CMFCD3D11TestView*)(pView);
			pCurView->SetNFGaussianSigma(fSigma);
		}

	}
	if(strPropName.Compare(_T("Line Scale")) == 0)
	{
		int iScale = pProp->GetValue().intVal;
		float fNewScale = (float)(iScale)/100.0f;
		CMainFrame* pFrame = (CMainFrame*)AfxGetMainWnd();
		CView* pView = pFrame->GetActiveFrame()->GetActiveView();
		if(pView)
		{
			CMFCD3D11TestView* pCurView = (CMFCD3D11TestView*)(pView);
			ID3D11Device* pDevice = AfxDXGlobal()->Device();
			pCurView->GetCurrentMainRender()->GetRenderHMTech()->SetNormalLineScale(pDevice,fNewScale);
		}
	}
	if(strPropName.Compare(_T("RTScaleRatio"))==0)
	{
		int iScaleVal = pProp->GetValue().intVal;
		CMainFrame* pFrame = (CMainFrame*)AfxGetMainWnd();
		CView* pView = pFrame->GetActiveFrame()->GetActiveView();
		
		if(pView){
			CMFCD3D11TestView* pCurView = (CMFCD3D11TestView*)(pView);
			CMFCD3D11TestDoc* pDoc = pCurView->GetDocument();
			ASSERT_VALID(pDoc);
			if (pDoc) 
			{
				float fScaleRatio = (float)(iScaleVal)/100.0f;
				pDoc->setRTScaleRatio(fScaleRatio);
			}
		}
	}
	if(strPropName.Compare(_T("RTThetaAng"))==0)
	{
		int iThetaVal = pProp->GetValue().intVal;
		CMainFrame* pFrame = (CMainFrame*)AfxGetMainWnd();
		CView* pView = pFrame->GetActiveFrame()->GetActiveView();

		if(pView){
			CMFCD3D11TestView* pCurView = (CMFCD3D11TestView*)(pView);
			CMFCD3D11TestDoc* pDoc = pCurView->GetDocument();
			ASSERT_VALID(pDoc);
			if (pDoc) 
			{
				float fThetaVal = (float)(iThetaVal);
				pDoc->setRTThetaAng(fThetaVal);
			}
		}
	}
	if(strPropName.Compare(_T("RTPhiAng"))==0)
	{
		int iPhiVal = pProp->GetValue().intVal;
		CMainFrame* pFrame = (CMainFrame*)AfxGetMainWnd();
		CView* pView = pFrame->GetActiveFrame()->GetActiveView();

		if(pView){
			CMFCD3D11TestView* pCurView = (CMFCD3D11TestView*)(pView);
			CMFCD3D11TestDoc* pDoc = pCurView->GetDocument();
			ASSERT_VALID(pDoc);
			if (pDoc) 
			{
				float fPhiVal = (float)(iPhiVal);
				pDoc->setRTThetaAng(fPhiVal);
			}
		}
	}
	if (strPropName.Compare(_T("FresnelReal"))==0)
	{
		CMainFrame* pFrame = (CMainFrame*)AfxGetMainWnd();
		CView* pView = pFrame->GetActiveFrame()->GetActiveView();
		if(pView){
			CMFCD3D11TestView* pCurView = (CMFCD3D11TestView*)(pView);
			CMFCD3D11TestDoc* pDoc = pCurView->GetDocument();
			ASSERT_VALID(pDoc);
			if (pDoc) 
			{
				_variant_t val = pProp->GetValue();
				val.ChangeType(VT_R8,NULL);
				pDoc->GetXYZFile()->setFresnelIORReal(val);
			}
		}
	}
	if (strPropName.Compare(_T("FresnelImg"))==0)
	{
		CMainFrame* pFrame = (CMainFrame*)AfxGetMainWnd();
		CView* pView = pFrame->GetActiveFrame()->GetActiveView();
		if(pView){
			CMFCD3D11TestView* pCurView = (CMFCD3D11TestView*)(pView);
			CMFCD3D11TestDoc* pDoc = pCurView->GetDocument();
			ASSERT_VALID(pDoc);
			if (pDoc) 
			{
				_variant_t val = pProp->GetValue();
				val.ChangeType(VT_R8,NULL);
				pDoc->GetXYZFile()->setFresnelIORImg(val);
			}
		}
	}
	if (strPropName.Compare(_T("a_x_GGX"))==0){
		CMainFrame* pFrame = (CMainFrame*)AfxGetMainWnd();
		CView* pView = pFrame->GetActiveFrame()->GetActiveView();
		if(pView){
			CMFCD3D11TestView* pCurView = (CMFCD3D11TestView*)(pView);
			_variant_t val = pProp->GetValue();
			val.ChangeType(VT_R8,NULL);
			pCurView->setAlphaX_AGGX(val);
		}
	}
	if (strPropName.Compare(_T("a_y_GGX"))==0){
		CMainFrame* pFrame = (CMainFrame*)AfxGetMainWnd();
		CView* pView = pFrame->GetActiveFrame()->GetActiveView();
		if(pView){
			CMFCD3D11TestView* pCurView = (CMFCD3D11TestView*)(pView);
			_variant_t val = pProp->GetValue();
			val.ChangeType(VT_R8,NULL);
			pCurView->setAlphaY_AGGX(val);
		}
	}
	if(strPropName.Compare(_T("Scaling_GGX"))==0){
		CMainFrame* pFrame = (CMainFrame*)AfxGetMainWnd();
		CView* pView = pFrame->GetActiveFrame()->GetActiveView();
		if(pView){
			CMFCD3D11TestView* pCurView = (CMFCD3D11TestView*)(pView);
			_variant_t val = pProp->GetValue();
			val.ChangeType(VT_R8,NULL);
			pCurView->setScalingFactor_AGGX(val);
		}
	}
	if(strPropName.Compare(_T("AjustScalingFactor"))==0){
		CMainFrame* pFrame = (CMainFrame*)AfxGetMainWnd();
		CView* pView = pFrame->GetActiveFrame()->GetActiveView();
		if(pView){
			CMFCD3D11TestView* pCurView = (CMFCD3D11TestView*)(pView);
			_variant_t val = pProp->GetValue();
			val.ChangeType(VT_R8);
			pCurView->setAdjustScalingFactor(val);
		}
	}
	if(strPropName.Compare(_T("HMWidth"))==0){
		CMainFrame* pFrame = (CMainFrame*)AfxGetMainWnd();
		CView* pView = pFrame->GetActiveFrame()->GetActiveView();
		if(pView){
			CMFCD3D11TestView* pCurView = (CMFCD3D11TestView*)(pView);
			unsigned int val = pProp->GetValue().uintVal;
			pCurView->setHMDataWidth(val);
		}
	}
	if(strPropName.Compare(_T("HMHeight"))==0){
		CMainFrame* pFrame = (CMainFrame*)AfxGetMainWnd();
		CView* pView = pFrame->GetActiveFrame()->GetActiveView();
		if(pView){
			CMFCD3D11TestView* pCurView = (CMFCD3D11TestView*)(pView);
			unsigned int val = pProp->GetValue().uintVal;
			pCurView->setHMDataHeight(val);
		}
	}
	if(strPropName.Compare(_T("DataPSizeX"))==0){
		CMainFrame* pFrame = (CMainFrame*)AfxGetMainWnd();
		CView* pView = pFrame->GetActiveFrame()->GetActiveView();
		if(pView){
			CMFCD3D11TestView* pCurView = (CMFCD3D11TestView*)(pView);
			_variant_t val = pProp->GetValue();
			val.ChangeType(VT_R8);
			pCurView->setDataPSizeX(val);
		}
	}
	if(strPropName.Compare(_T("DataPSizeY"))==0){
		CMainFrame* pFrame = (CMainFrame*)AfxGetMainWnd();
		CView* pView = pFrame->GetActiveFrame()->GetActiveView();
		if(pView){
			CMFCD3D11TestView* pCurView = (CMFCD3D11TestView*)(pView);
			_variant_t val = pProp->GetValue();
			val.ChangeType(VT_R8);
			pCurView->setDataPSizeY(val);
		}
	}		
	if(strPropName.Compare(_T("1D_Offset_X")) == 0)
	{
		int iOffsetY = pProp->GetValue().intVal;
		CMainFrame* pFrame = (CMainFrame*)AfxGetMainWnd();
		CView* pView = pFrame->GetActiveFrame()->GetActiveView();
		if(pView)
		{
			CMFCD3D11TestView* pCurView = (CMFCD3D11TestView*)(pView);
			CRenderHeightMap* pHMTech = pCurView->GetCurrentMainRender()->GetRenderHMTech();
			if(pHMTech)
			{
				CXYZFileIO* pXYZFile = pHMTech->GetCurXYZFile();
				if(pXYZFile)
				{
					float fOffsetY = (float)(iOffsetY)/pXYZFile->GetCur2DNDFImgDim();			
					pCurView->m_dlg2DNDFVisual.m_wndBMP.Set1DSliceOffset_X(fOffsetY);
				}
			}
		}
	}
	if(strPropName.Compare(_T("1D_Offset_Y")) == 0)
	{
		int iOffsetX = pProp->GetValue().intVal;
		CMainFrame* pFrame = (CMainFrame*)AfxGetMainWnd();
		CView* pView = pFrame->GetActiveFrame()->GetActiveView();
		if(pView)
		{
			CMFCD3D11TestView* pCurView = (CMFCD3D11TestView*)(pView);
			CRenderHeightMap* pHMTech = pCurView->GetCurrentMainRender()->GetRenderHMTech();
			if(pHMTech)
			{
				CXYZFileIO* pXYZFile = pHMTech->GetCurXYZFile();
				if(pXYZFile)
				{
					float fOffsetX = (float)(iOffsetX)/pXYZFile->GetCur2DNDFImgDim();			
					pCurView->m_dlg2DNDFVisual.m_wndBMP.Set1DSliceOffset_Y(fOffsetX);
				}
			}
		}
	}
	if(strPropName.Compare(_T("1D_Rotate_X")) == 0)
	{
		int iRotateAngle = pProp->GetValue().intVal;
		float fRotateAngle = (float)(iRotateAngle)/10.0f; //degrees to radians
		CMainFrame* pFrame = (CMainFrame*)AfxGetMainWnd();
		CView* pView = pFrame->GetActiveFrame()->GetActiveView();
		if(pView)
		{
			CMFCD3D11TestView* pCurView = (CMFCD3D11TestView*)(pView);
			pCurView->m_dlg2DNDFVisual.m_wndBMP.Set1DSliceRotation_X(fRotateAngle);
		}
	}
	if(strPropName.Compare(_T("1D_Rotate_Y")) == 0)
	{
		int iRotateAngle = pProp->GetValue().intVal;
		float fRotateAngle = (float)(iRotateAngle)/10.0f; //degrees to radians
		CMainFrame* pFrame = (CMainFrame*)AfxGetMainWnd();
		CView* pView = pFrame->GetActiveFrame()->GetActiveView();
		if(pView)
		{
			CMFCD3D11TestView* pCurView = (CMFCD3D11TestView*)(pView);
			pCurView->m_dlg2DNDFVisual.m_wndBMP.Set1DSliceRotation_Y(fRotateAngle);
		}
	}
	if(strPropName.Compare(_T("Show Normal")) == 0)
	{
		bool bRender = (pProp->GetValue().boolVal == VARIANT_TRUE)? true:false;
		CMainFrame* pFrame = (CMainFrame*)AfxGetMainWnd();
		CView* pView = pFrame->GetActiveFrame()->GetActiveView();
		if(pView)
		{
			CMFCD3D11TestView* pCurView = (CMFCD3D11TestView*)(pView);
			ID3D11Device* pDevice = AfxDXGlobal()->Device();
			pCurView->GetCurrentMainRender()->GetRenderHMTech()->SetbRenderNormal(bRender);
		}
	}
	if(strPropName.Compare(_T("1D Gonio Dir")) == 0){
		CString strTmp = pProp->GetValue();
		GONIOAXIS iWhichAxis;
		if( strTmp.Compare(_T("MAJORAXIS"))  == 0) 
			iWhichAxis = MAJORAXIS;
		else if (strTmp.Compare(_T("MINORAXIS")) == 0)
			iWhichAxis = MINORAXIS;
		
		CMainFrame* pFrame = (CMainFrame*)AfxGetMainWnd();
		CView* pView = pFrame->GetActiveFrame()->GetActiveView();
			if(pView) {
				CMFCD3D11TestView* pCurView = (CMFCD3D11TestView*)(pView);
				CMFCD3D11TestDoc* pDoc = pCurView->GetDocument();
				if(pDoc)
					pDoc->GetXYZFile()->SetCurGonioDir(iWhichAxis);
			}		 
	}

	if (strPropName.Compare(_T("Estimation Method")) == 0)
	{
		CString strTmp = pProp->GetValue();
		if(strTmp.Compare(_T("LS PCA")) == 0)
		{
			CMainFrame* pFrame = (CMainFrame*)AfxGetMainWnd();
			CView* pView = pFrame->GetActiveFrame()->GetActiveView();
			if(pView)
			{
				CMFCD3D11TestView* pCurView = (CMFCD3D11TestView*)(pView);
				ID3D11Device* pDevice = AfxDXGlobal()->Device();
				CRenderHeightMap* pHMTech = pCurView->GetCurrentMainRender()->GetRenderHMTech();
				if(pHMTech)
				{
					CXYZFileIO* pXYZFile = pHMTech->GetCurXYZFile();
					if(pXYZFile)
					{
						pXYZFile->SetNormalEstiMethod(USEPCANORMAL);
						pHMTech->prepareNormalLineVB(pDevice);
						pCurView->m_dlg2DNDFVisual.SetBitmap((LPCTSTR)pCurView->m_dlg2DNDFVisual.m_wndBMP.GetCurNDFFileFullName());

						//Change GUI
						CMFCPropertyGridProperty* pXYZNormalProperty = theApp.GetPropertyGridCtrl()->GetProperty(2);
						CMFCPropertyGridProperty* pNormalWndSize = pXYZNormalProperty->GetSubItem(1);
						CMFCPropertyGridProperty* pNFGaussianSigma = pXYZNormalProperty->GetSubItem(2);

						pNormalWndSize->EnableSpinControl(TRUE,3,99);
						pNFGaussianSigma->EnableSpinControl(FALSE);

					}

				}

			}
		}
		else if(strTmp.Compare(_T("LS PCA Gauss")) == 0)
		{
			CMainFrame* pFrame = (CMainFrame*)AfxGetMainWnd();
			CView* pView = pFrame->GetActiveFrame()->GetActiveView();
			if(pView)
			{
				CMFCD3D11TestView* pCurView = (CMFCD3D11TestView*)(pView);
				ID3D11Device* pDevice = AfxDXGlobal()->Device();
				CRenderHeightMap* pHMTech = pCurView->GetCurrentMainRender()->GetRenderHMTech();
				if(pHMTech)
				{
					CXYZFileIO* pXYZFile = pHMTech->GetCurXYZFile();
					if(pXYZFile)
					{
						pXYZFile->SetNormalEstiMethod(USEWEIGHTEDPCA_GAUSSIAN);
						pHMTech->prepareNormalLineVB(pDevice);
						pCurView->m_dlg2DNDFVisual.SetBitmap((LPCTSTR)pCurView->m_dlg2DNDFVisual.m_wndBMP.GetCurNDFFileFullName());

						//Change GUI
						CMFCPropertyGridProperty* pXYZNormalProperty = theApp.GetPropertyGridCtrl()->GetProperty(2);
						CMFCPropertyGridProperty* pNormalWndSize = pXYZNormalProperty->GetSubItem(1);
						CMFCPropertyGridProperty* pNFGaussianSigma = pXYZNormalProperty->GetSubItem(2);

						pNormalWndSize->EnableSpinControl(FALSE);
						pNFGaussianSigma->EnableSpinControl(TRUE,1,100);
					}


				}

			}
		}
		else if(strTmp.Compare(_T("TriArea Avg")) == 0)
		{
			CMainFrame* pFrame = (CMainFrame*)AfxGetMainWnd();
			CView* pView = pFrame->GetActiveFrame()->GetActiveView();
			if(pView)
			{
				CMFCD3D11TestView* pCurView = (CMFCD3D11TestView*)(pView);
				ID3D11Device* pDevice = AfxDXGlobal()->Device();
				CRenderHeightMap* pHMTech = pCurView->GetCurrentMainRender()->GetRenderHMTech();
				if(pHMTech)
				{
					CXYZFileIO* pXYZFile = pHMTech->GetCurXYZFile();
					if(pXYZFile)
					{
						pXYZFile->SetNormalEstiMethod(USEAREAWEIGHTEDNORMAL);
						pHMTech->prepareNormalLineVB(pDevice);
						pCurView->m_dlg2DNDFVisual.SetBitmap((LPCTSTR)pCurView->m_dlg2DNDFVisual.m_wndBMP.GetCurNDFFileFullName());

						//Change GUI
						CMFCPropertyGridProperty* pXYZNormalProperty = theApp.GetPropertyGridCtrl()->GetProperty(2);
						CMFCPropertyGridProperty* pNormalWndSize = pXYZNormalProperty->GetSubItem(1);
						CMFCPropertyGridProperty* pNFGaussianSigma = pXYZNormalProperty->GetSubItem(2);

						pNormalWndSize->EnableSpinControl(FALSE);
						pNFGaussianSigma->EnableSpinControl(FALSE);
					}

				}

			}
		}
		theApp.OutputString(strTmp);
	}

	if(strPropName.Compare(_T("Wireframe")) == 0)
	{
		bool bWF = (pProp->GetValue().boolVal == VARIANT_TRUE)?true:false;
		CMainFrame* pFrame = (CMainFrame*)AfxGetMainWnd();
		CView* pView = pFrame->GetActiveFrame()->GetActiveView();
		if(pView)
		{
			CMFCD3D11TestView* pCurView = (CMFCD3D11TestView*)(pView);
			ID3D11Device* pDevice = AfxDXGlobal()->Device();
			pCurView->GetCurrentMainRender()->GetRenderHMTech()->SetbRenderWF(bWF);
		}
	}

	if(strPropName.Compare(_T("Avg1DNDF")) == 0)
	{
		bool bAvg = (pProp->GetValue().boolVal == VARIANT_TRUE)?true:false;
		CMainFrame* pFrame = (CMainFrame*)AfxGetMainWnd();
		CView* pView = pFrame->GetActiveFrame()->GetActiveView();
		if(pView)
		{
			CMFCD3D11TestView* pCurView = (CMFCD3D11TestView*)(pView);
			//pCurView->GetCurrentMainRender()->GetRenderHMTech()->GetCurXYZFile()->SetbAvg1DNDF(bAvg);
			pCurView->SetbAvg1DNDF(bAvg);
		}
	}

	return 1;
}